/**
 * Created by hubin on 2016/6/14.
 */

var product_vm = avalon.define({
    $id: "product",
    pageSize: 6,
    pageCurrent: 1,
    pageTotle: 0,
    productlist: {},
    keyword: "",
    productItem: {id: 0, rate: 0.0, type: "", title: "", introduce: "", createDate: "", userId: 0},
    updateItem: {id: 0, rate: 0.0, type: "", title: "", introduce: "", createDate: "", userId: 0},

    <!-- 获取理产品列表-->
    getProductList: function () {
        $('#addtproductime').datepicker({startDate: '2015-5-5', format: 'yyyy-mm-dd'});
        $.ajax({
            type: 'POST',
            url: '/CounterDrops/product/getproductlist',
            data: {
                pageSize: product_vm.pageSize,
                pageCurrent: product_vm.pageCurrent,
                keyword: product_vm.keyword
            },
            success: function (data) {
                product_vm.productlist = data.model.results;
                product_vm.pageTotle = data.model.totalPage;
            },
            error: function (e) {

            }
        })
    },
    cheak: function () {
        product_vm.pageCurrent = 1;
        product_vm.getProductList();
    },

    firstPage: function () {
        product_vm.pageCurrent = 1;
        product_vm.getProductList();
    },
    lastPage: function () {
        product_vm.pageCurrent = product_vm.pageTotle;
        product_vm.getProductList();
    },
    previousPage: function () {
        if (product_vm.pageCurrent == 1) {
            swal("当前为第一页!");
            return;
        }
        product_vm.pageCurrent--;
        product_vm.getProductList();

    },
    nextPage: function () {
        if (product_vm.pageCurrent == product_vm.pageTotle) {

            swal("当前为最后一页!");
            return;
        }
        product_vm.pageCurrent++;
        product_vm.getProductList();
    },
    update: function (el) {
        product_vm.updateItem = el;
        product_vm.updateItem.createDate = new Date(el.createDate).toString("yyyy-MM-dd");
        $('#updateModel').modal('show');
        $('#EndDate').datepicker({startDate: '2015-5-5', format: 'yyyy-mm-dd'});
    },

    updateProduct: function () {
        $.ajax({
            type: 'POST',
            url: '/CounterDrops/product/updaeproduct',
            data: {
                title: product_vm.updateItem.title,
                rate: product_vm.updateItem.rate,
                createDate: product_vm.updateItem.createDate,
                introduce: product_vm.updateItem.introduce,
                ProductID: product_vm.updateItem.id
            },
            success: function (data) {


                if (data.success) {
                    swal("修改成功!");
                    product_vm.getProductList();
                    $('#updateModel').modal('hide');
                } else {
                    swal("修改失败!");
                }
            },
            error: function (e) {

            }
        })
    },

    delProduct: function () {

        $.ajax({
            type: 'GET',
            url: '/CounterDrops/product/delproduct',
            data: {
                ProductID: product_vm.updateItem.id
            },
            success: function (data) {

                if (data.success) {
                    swal("删除成功!");
                    product_vm.getProductList();
                } else {
                    swal("删除失败!");
                }
            },
            error: function (e) {

            }
        })
    },
    Took: function () {
        $.ajax({
            type: 'POST',
            url: '/CounterDrops/product/tookproduct',
            data: {
                ProductID: product_vm.updateItem.id,

            },
            success: function (data) {

                if (data.success) {
                    swal("下架成功!");
                    product_vm.getProductList();
                } else {
                    swal("下架失败!");
                }
                $('#myModal').modal('hide');

            },
            error: function (e) {

            }
        })
    },

    addProduct: function () {
        $.ajax({
            type: 'POST',
            url: '/CounterDrops/product/addproduct',
            data: {
                title: product_vm.productItem.title,
                rate: product_vm.productItem.rate,
                createDate: product_vm.productItem.createDate,
                introduce: product_vm.productItem.introduce
            },
            success: function (data) {

                if (data.success) {
                    swal("添加成功!");
                    product_vm.getProductList();
                } else {
                    swal("添加失败!");
                }
                $('#myModal').modal('hide');

            },
            error: function (e) {

            }
        })
    }
});
product_vm.getProductList();